package cn.sxt.logistics.controller;


import cn.sxt.logistics.pojo.*;
import cn.sxt.logistics.service.IAdminService;
import cn.sxt.logistics.service.IBaseDataService;
import cn.sxt.logistics.service.ICustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-17
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IAdminService adminService;
    
    
    @Autowired
    private IBaseDataService baseDataService;



    @GetMapping("/customerManager")
    public String customerManager(){
        return "customerManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(
            @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize
            , String keyword){

        //分页对象
        Page<Customer> customerPage = new Page<>(pageNum,pageSize);
        //条件对象
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();

        /**
         * 获取当前身份的角色名称
         * 如果是业务员
         * 就只能查看自己的的客户
         * */
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin)subject.getPrincipal();
        if("业务员".equals(admin.getRolename())){
            queryWrapper.eq("admin_id",admin.getAdminId());
        }


        /*关键字*/
        if(StringUtils.isNotBlank(keyword)){

            //客户名称模糊查询
            queryWrapper.like("customer_name",keyword);

            queryWrapper.or();

            //业务员名称模糊查询
            queryWrapper.like("salesman",keyword);
        }
        //查询结果
        IPage<Customer> result = customerService.selectCustomers(customerPage, queryWrapper);

        return new ResultObject(result);
    }


    //删除功能
    @GetMapping("/delete")
    @ResponseBody
    public MessageObject delete(Long customerId){

        boolean flag = customerService.removeById(customerId);
        MessageObject mo = new MessageObject(1, "删除数据成功");
        if(!flag){
            mo = new MessageObject(0,"删除数据失败，请联系管理员");
        }
        return mo;
    }


    /*跳转到编辑页面*/
    @GetMapping("/customerEdit")
    public String customerEdit(Model m, Long customerId){


        /*查询出所有业务员*/

        //创建条件对象
        QueryWrapper<Map<String,Object>> queryWrapper = new QueryWrapper<>();
        //查询所有业务员
        queryWrapper.eq("rolename","业务员");
        //执行Admin查询方法
        List<Admin> admins = adminService.selectAdmins(queryWrapper);
        //共享业务员
        m.addAttribute("admins",admins);
        
        /*查询所有区间数据：基础数据表*/
        List<BaseData> regions = baseDataService.selectBaseDatasByParentName("区间管理");
        /*共享区间数据*/
        m.addAttribute("regions",regions);

        //如果有id说明是修改操作
        if(customerId !=null){
            Customer customer = customerService.getById(customerId);
            m.addAttribute("customer",customer);
        }



        return "customerEdit";
    }



    /*新增数据*/
    @PostMapping("/insert")
    @ResponseBody
    public MessageObject insert(Customer customer){

        //新增管理员
        boolean flag = customerService.saveOrUpdate(customer);


        MessageObject mo = new MessageObject(1, "新增数据成功");
        if(!flag){
            mo = new MessageObject(0,"新增数据失败，请联系管理员");
        }
        return mo;
    }



    /*修改数据*/
    @PostMapping("/update")
    @ResponseBody
    public MessageObject update(Customer customer){
        //新增管理员
        boolean flag = customerService.saveOrUpdate(customer);

        MessageObject mo = new MessageObject(1, "修改数据成功");
        if(!flag){
            mo = new MessageObject(0,"修改数据失败，请联系管理员");
        }
        return mo;
    }
}

